﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Diagnostics.CodeAnalysis
Imports System.Workflow.ComponentModel

Namespace System.Workflow.Activities.Rules
    Public NotInheritable Class RuleDefinitions
        Implements IWorkflowChangeDiff
        ' Methods
        Friend Function Clone() As RuleDefinitions
            Dim definitions As New RuleDefinitions
            If (Not Me.ruleSets Is Nothing) Then
                definitions.ruleSets = New RuleSetCollection
                Dim set As RuleSet
                For Each set In Me.ruleSets
                    definitions.ruleSets.Add([set].Clone)
                Next
            End If
            If (Not Me.conditions Is Nothing) Then
                definitions.conditions = New RuleConditionCollection
                Dim condition As RuleCondition
                For Each condition In Me.conditions
                    definitions.conditions.Add(condition.Clone)
                Next
            End If
            Return definitions
        End Function

        Public Function Diff(ByVal originalDefinition As Object, ByVal changedDefinition As Object) As IList([Of] WorkflowChangeAction)
            Dim definitions As RuleDefinitions = [TryCast](originalDefinition,RuleDefinitions)
            Dim definitions2 As RuleDefinitions = [TryCast](changedDefinition,RuleDefinitions)
            If ((definitions Is Nothing) OrElse (definitions2 Is Nothing)) Then
                Return New List([Of] WorkflowChangeAction)
            End If
            Dim list As IList([Of] WorkflowChangeAction) = Me.Conditions.Diff(definitions.Conditions, definitions2.Conditions)
            Dim list2 As IList([Of] WorkflowChangeAction) = Me.RuleSets.Diff(definitions.RuleSets, definitions2.RuleSets)
            If (list.Count = 0) Then
                Return list2
            End If
            Dim i As Integer
            For i = 0 To list2.Count - 1
                list.Add(list2.Item(i))
            Next i
            Return list
        End Function

        Friend Shared Function OnGetRuleConditions(ByVal dependencyObject As DependencyObject) As Object
            If (dependencyObject Is Nothing) Then
                Throw New ArgumentNullException("dependencyObject")
            End If
            Dim valueBase As RuleDefinitions = [TryCast](dependencyObject.GetValueBase(RuleDefinitions.RuleDefinitionsProperty),RuleDefinitions)
            If (valueBase Is Nothing) Then
                Dim activity As Activity = [TryCast](dependencyObject,Activity)
                If (activity.Parent Is Nothing) Then
                    valueBase = ConditionHelper.GetRuleDefinitionsFromManifest(activity.GetType)
                    If (Not valueBase Is Nothing) Then
                        dependencyObject.SetValue(RuleDefinitions.RuleDefinitionsProperty, valueBase)
                    End If
                End If
            End If
            Return valueBase
        End Function

        Friend Sub OnRuntimeInitialized()
            SyncLock Me.syncLock
                If Not Me.runtimeInitialized Then
                    Me.Conditions.OnRuntimeInitialized
                    Me.RuleSets.OnRuntimeInitialized
                    Me.runtimeInitialized = True
                End If
            End SyncLock
        End Sub


        ' Properties
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public ReadOnly Property Conditions As RuleConditionCollection
            Get
                If (Me.conditions Is Nothing) Then
                    Me.conditions = New RuleConditionCollection
                End If
                Return Me.conditions
            End Get
        End Property

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public ReadOnly Property RuleSets As RuleSetCollection
            Get
                If (Me.ruleSets Is Nothing) Then
                    Me.ruleSets = New RuleSetCollection
                End If
                Return Me.ruleSets
            End Get
        End Property


        ' Fields
        Private conditions As RuleConditionCollection
        <SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")> _
        Public Shared ReadOnly RuleDefinitionsProperty As DependencyProperty = DependencyProperty.RegisterAttached("RuleDefinitions", GetType(RuleDefinitions), GetType(RuleDefinitions), New PropertyMetadata(Nothing, DependencyPropertyOptions.Metadata, New GetValueOverride(AddressOf RuleDefinitions.OnGetRuleConditions), Nothing, New Attribute() { New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
        Private ruleSets As RuleSetCollection
        Private runtimeInitialized As Boolean
        <NonSerialized> _
        Private [syncLock] As Object = New Object
    End Class
End Namespace

